package me.xiaosiqi.server_info.controller;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import me.xiaosiqi.server_info.comm.BaseResponse;
import me.xiaosiqi.server_info.comm.Command;
import me.xiaosiqi.server_info.comm.Service;
import me.xiaosiqi.server_info.mapper.CommandMapper;
import me.xiaosiqi.server_info.mapper.ServiceMapper;
import me.xiaosiqi.server_info.tool.IpTool;
import me.xiaosiqi.server_info.tool.IpVersionCheckTool;
import me.xiaosiqi.server_info.tool.ServerChanService;
import me.xiaosiqi.server_info.tool.ServerDescTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.Map;

/**
 * @Author: xiaosiqi
 * @date 2021/11/11 10:49
 */
@Slf4j
@RestController
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    private ServiceMapper serviceMapper;

    //网卡名
    @Value("${spring.ethName}")
    private String ethName;


    @PostMapping("/ipList")
    public BaseResponse ipList() {
        Map<String, List<String>> ipInfo = IpTool.getIpInfo();
        List<String> ipList = ipInfo.get(ethName);
        if (CollectionUtil.isEmpty(ipList)){
            return BaseResponse.error("网卡名称错误，请检查网卡名是否配置正确！");
        }
        return BaseResponse.ok(ipList);
    }
    @PostMapping("/serviceList")
    public BaseResponse serviceList(@RequestParam Integer  id) {
        List<Service> selectList = serviceMapper.selectList(null);
        Map<String, List<String>> ipInfo = IpTool.getIpInfo();
        List<String> ipList = ipInfo.get(ethName);
        if (CollectionUtil.isEmpty(ipList)){
            return BaseResponse.error("网卡名称错误，请检查网卡名是否配置正确！");
        }
        if (id>ipList.size()-1){
            return BaseResponse.error("id错误");
        }
        String ip = ipList.get(id);
        for (int i = 0; i < selectList.size(); i++) {
            String iPVersion = IpVersionCheckTool.checkIPVersion(ip);
            if (iPVersion.equals("IPv6")){
                selectList.get(i).setIp("["+ip+"]");
            }else {
                selectList.get(i).setIp(ip);
            }
            Socket socket = new Socket();
            try {
                socket = new Socket();
                Integer port = selectList.get(i).getPort();
                socket.connect(new InetSocketAddress(ip, port),10);
                selectList.get(i).setIsNormal(1);
            } catch (IOException e) {
                selectList.get(i).setIsNormal(0);
            }finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return BaseResponse.ok(selectList);
    }
}